from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from fields import PickledObjectField
import datetime

 
class UserProfile(models.Model):
    user = models.OneToOneField(User)
    bankAccountNumber = models.CharField(max_length=75)
    #activation_key = models.CharField(max_length = 32)
    #key_expires = models.DateTimeField()
    def __unicode__(self):
        return self.user.username
    
    def create_user_profile(sender, instance, created, **kwargs):
        if created:
            UserProfile.objects.create(user=instance)
    
    post_save.connect(create_user_profile, sender=User)
    

class EventProfile(models.Model):
    eventStartDate = models.DateField(max_length = 32)
    eventEndDate = models.DateField(max_length = 32)
    eventName = models.CharField(max_length=75)
    def __unicode__(self):
        return self.eventName
    
    #def GetDatesList(self):
    #    List = []
    #    delta = 0
    #    while (self.eventStartDate + datetime.timedelta(delta) !=
    #           self.eventEndDate + datetime.timedelta(1)):
    #        List.append(self.eventStartDate + datetime.timedelta(delta))
    #        delta += 1
    #        
    #    return List
    #
    #def UpdatePeopleOnEvent(self, Ids):
    #    #want to check if there are any new users,
    #    #Check if any one needs removing
    #    # update proportions
    #    EventDates = self.GetDatesList()
    #    Changed = False
    #    UsersOnEvent = self.persononevent_set.all()
    #    UserIdsOnEvent = [u.user.id for u in UsersOnEvent]
    #    #FirstCheck if there is any difference
    #    #print Ids
    #    UsersToAdd = Ids[:]
    #    UsersToRemove = []
    #    
    #    for id in UserIdsOnEvent:
    #        if str(id) in Ids:
    #            UsersToAdd.remove(str(id))
    #        else:
    #            UsersToRemove.append(id)
    #    # Add New Users:
    #    for Id in UsersToAdd:
    #        Changed = True
    #        U = UserProfile.objects.get(id = int(Id))
    #        P = PersonOnEvent(user = U,
    #                  userDatesOnEvent = (EventDates,[100]*len(EventDates)),
    #                  userProportion = 1.0,
    #                  Event = self)
    #        P.save()
    #    #Remove People:    
    #    for Id in UsersToRemove:
    #        Changed = True
    #        PersonOnEvent.objects.filter(user__id = Id).filter(Event__id = self.id).delete()    
    #    #Makesure everyones Dates Are up to scratch
    #    if Changed:
    #        UsersOnEvent = self.persononevent_set.all()
    #    for U in UsersOnEvent:
    #        if U.userDatesOnEvent[0] != EventDates:
    #            print "Saving New Dates"
    #            U.userDatesOnEvent = (EventDates,[100]*len(EventDates))
    #            U.save()
    #    #Recalculate Proportions
    #    
    #def IsIdOnEvent(self, id):
    #    try:
    #        Person = self.persononevent_set.get(user__id = id)
    #    except PersonOnEvent.DoesNotExist:
    #        Person = None
    #    return Person
    #
    #
    #def GetTotalEventExpenses(self):
    #    Expenses = self.eventexpenseprofile_set.all()
    #    Total = 0.0
    #    for Expense in Expenses:
    #        Total += Expense.expenseAmount
    #    return Total
    #        
    #    
    #    

class SubgroupExpense(models.Model):
    subgroupExpName = models.CharField(max_length = 32)
    subgroupexpenseAmount = models.FloatField()
    subgroupexpenseDate = models.DateField()
    subgroupexpenseAddedDate = models.DateTimeField()
    subgroupexpenseOwner = models.ForeignKey(UserProfile)
    #subgroupexpenseCreator = models.ForeignKey(UserProfile)
    subgroupexpenseEvent = models.ForeignKey(EventProfile, blank = True, null = True)
    
class PersonInSubgroup(models.Model):
    subgroupuser = models.ForeignKey(UserProfile)
    subgroupuserProportion = models.FloatField()
    Subgroup = models.ForeignKey(EventProfile)
    
class EventExpenseProfile(models.Model):
    expenseName = models.CharField(max_length = 32)
    expenseAmount = models.FloatField() 
    expenseDate = models.DateField()
    expenseAddedDate = models.DateTimeField()
    expenseOwner = models.ForeignKey(UserProfile)
    expenseEvent = models.ForeignKey(EventProfile, blank = True, null = True)
    
    def __unicode__(self):
        return self.expenseName
    
class PersonOnEvent(models.Model):
    user = models.ForeignKey(UserProfile)
    userDatesOnEvent = PickledObjectField()
    userProportion = models.FloatField()
    Event = models.ForeignKey(EventProfile)
    
    def __unicode__(self):
        return self.user.user.username
    
    #def UpdateDatesAttended(self, DatesAttended):
    #    Dates = self.userDatesOnEvent[0]
    #    Proportions = []
    #    Proportion = 0
    #    Total = 0
    #    i = 0
    #    while i < len(Dates):
    #        if str(Dates[i]) in DatesAttended:
    #            Proportions.append(100)
    #            Proportion += 100        
    #        else:
    #            Proportions.append(0)
    #        Total += 100
    #        i += 1
    #    
    #    #print (1.0*Proportion)/(1.0*Total)
    #    self.userDatesOnEvent = (Dates,Proportions)
    #    self.userProportion = (1.0*Proportion)/(1.0*Total)
    #    self.save()
        
            
        
    

    
    
       